package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.UploadingEntity;
import com.xilihui.xlh.business.entities.UserEntity;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.business.requests.SignedRequest;
import com.xilihui.xlh.component.ipicker.IPicker;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/4.
 */

public class AccountInfoActivity extends BaseCompatActivity implements IPicker.OnSelectedListener {
    
    @BindView(R.id.sdv_head)
    SimpleDraweeView sdv;
    @BindView(R.id.tv_nick)
    TextView tv_nick;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_sign)
    TextView tv_sign;
    
    @BindView(R.id.ll_namde)
    LinearLayout ll_name;
    @BindView(R.id.view_line1)
    View view1;
    @BindView(R.id.ll_address)
    LinearLayout ll_address;
    @BindView(R.id.view_line2)
    View view2;
    
    Intent intent;
    
    @Override
    public int getLayout() {
        return R.layout.activity_account_info;
    }

    @Override
    public String initTitle() {
        return "账号信息";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
    }

    @Override
    public void initData() {
        getData();
    }
    
    
    public void getData(){
        MineRequest.userInfo(this)
                .compose(DoTransform.<UserEntity>applyScheduler(this,true))
                .compose(AccountInfoActivity.this.<UserEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<UserEntity>(this) {
                    @Override
                    protected void onSuccess(UserEntity entity) {
                        
                        sdv.setImageURI(UrlConst.baseUrl()+entity.getData().getHead_pic());
                        tv_nick.setText(entity.getData().getNickname());
                        tv_name.setText(entity.getData().getReal_name());
                        tv_address.setText(entity.getData().getAddr());
                        tv_sign.setText(entity.getData().getSignature());
                        YEventBuses.post(new YEventBuses.Event("Mine"));
                        if(!entity.getData().getSign_status().equals("3")){
                            view1.setVisibility(View.GONE);
                            view2.setVisibility(View.GONE);
                            ll_name.setVisibility(View.GONE);
                            ll_address.setVisibility(View.GONE);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    @OnClick(R.id.ll_headImage)
    public void headImage(){
        IPicker iPicker = IPicker.getInstance();
        iPicker.setLimit(1);
        iPicker.setCropEnable(true);
        iPicker.setOnSelectedListener(this);
        iPicker.open(this);
    }
    
    @OnClick(R.id.ll_nick)
    public void nick(){
        intent = new Intent(AccountInfoActivity.this,EditActivity.class);
        intent.putExtra("type",1);
        startActivity(intent);
    }
    
    @OnClick(R.id.ll_sign)
    public void sign(){
        intent = new Intent(AccountInfoActivity.this,EditActivity.class);
        intent.putExtra("type",2);
        startActivity(intent);
    }


    @Override
    public void onSelected(List<String> paths) {
        upload(paths.get(0));
    }

    public void upload(String url){
        SignedRequest.uploadImgAndCompress(this,url)
                .compose(DoTransform.<UploadingEntity>applyScheduler(this,true))
                .compose(AccountInfoActivity.this.<UploadingEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<UploadingEntity>(this) {
                    @Override
                    protected void onSuccess(UploadingEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        final String url = entity.getImg_url();
                        MineRequest.userSave(AccountInfoActivity.this,"head_pic",
                                url)
                                .compose(DoTransform.<BaseEntity>applyScheduler(AccountInfoActivity.this,true))
                                .compose(AccountInfoActivity.this.<BaseEntity>bindToLifecycle())
                                .subscribe(new HttpSubscriber<BaseEntity>(AccountInfoActivity.this) {
                                    @Override
                                    protected void onSuccess(BaseEntity entity) {
                                        ToastUtil.toastShortPositive(entity.getMsg());
                                        sdv.setImageURI(UrlConst.baseUrl()+url);
                                    }
                                    
                                    @Override
                                    protected void onFail(String msg) {
                                        ToastUtil.toastShortPositive(msg);
                                    }
                                });
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortPositive(msg);
                    }
                    
                    
                });
        
        
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is("sx")){
            getData();
        }
    }
    
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
